package com.example.javamaday.leetcode;

import java.util.HashMap;
import java.util.Map;

public class LeetCodeTest0283 {

    public static void main(String[] args) {
        new LeetCodeTest0283().moveZeroes(new int[]{0, 1, 0, 3, 12});
    }

    public void moveZeroes(int[] nums) {
        int currentZeros = 0;
        for (int i = 0; i < nums.length; i++) {
            int temp = nums[i];
            if (temp == 0) {
                currentZeros++;
            } else {
                if (currentZeros > 0) {
                    nums[i] = 0;
                }
                nums[i - currentZeros] = temp;
            }
        }
    }

    public void moveZeroes2Loop(int[] nums) {
        Map<Integer, Integer> numMap = new HashMap<>();
        int currentZeros = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                currentZeros++;
            }
            numMap.put(i, currentZeros);
        }
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[i - numMap.get(i)] = nums[i];
                index++;
            }
        }
        while (index <= nums.length) {
            nums[index] = 0;
        }
    }

}
